/*
 * @Author: aggfb 714740235@qq.com
 * @Date: 2024-04-16 22:43:11
 * @LastEditors: aggfb 714740235@qq.com
 * @LastEditTime: 2024-04-18 22:48:18
 * @FilePath: \stepper_motor\bsp\bsp_adc.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "bsp_adc.h"

/**
 *  adc0 初始化
 *  注入组循环采样
*/
void bsp_adc0_init(void)
{
    rcu_periph_clock_enable(RCU_ADC0);
    rcu_periph_clock_enable(RCU_GPIOC);

    rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV4);

    //PC0 ---> 电源电压
    //PC1 ---> 电源电流
    //PC2 ---> 模拟量检测
    gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_MAX, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2); 

    adc_mode_config(ADC_MODE_FREE);
    adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);//ADC扫描模式
    adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);//ADC连续转换模式开启

    adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT); //右对齐


    adc_channel_length_config(ADC0, ADC_INSERTED_CHANNEL, 3); //注入组通道长度
    adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_0, ADC_SAMPLETIME_55POINT5);
    adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_1, ADC_SAMPLETIME_55POINT5);
    adc_inserted_channel_config(ADC0, 2, ADC_CHANNEL_2, ADC_SAMPLETIME_55POINT5);

    adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_2_EXTTRIG_INSERTED_NONE); //软件触发
    adc_external_trigger_config(ADC0, ADC_INSERTED_CHANNEL, ENABLE); //外部触发使能

    adc_enable(ADC0);
    delay_1ms(1);
    adc_calibration_enable(ADC0);
}


